const Websocket = require('ws');
const path = require('path');
const getFileData = require('./utils/readFile')
const server = new Websocket.Server({
    port:8888
});
module.exports.listen = ()=>{
    server.on('connection',client=>{
        console.log('后台连接成功')
        client.on('message', async msg=>{
         const payload = JSON.parse(msg)
         const {action,chartName,value} = payload;
         if(action=='getData'){
            let filePath = './data/'+chartName+'.json'
            filePath = path.resolve(__dirname,filePath)
            let data = await getFileData(filePath)
            payload.data = data;
            client.send(JSON.stringify(payload))
         }else{
            server.clients.forEach(c=>{
                c.send(JSON.stringify(payload))
            })
         }
        })
    })
}